package com.tg.app.activity.device.settings;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.appbase.custom.constant.CommonConstants;
import com.base.BaseActivity;
import com.tange.base.toolkit.StringUtils;
import com.tange.core.device.manage.version.DeviceFirmware;
import com.tange.core.device.manage.version.FirmwareVersion;
import com.tg.app.R;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;


public class DeviceInfoActivity extends BaseActivity {
    public static final String TAG = "DeviceInfoActivity";
    public static final String EXT_DEVICE_NAME = "ext_device_name";
    public static final String EXT_DEVICE_NAME_TYPE = "ext_device_name_type";
    private TextView idtv;
    private TextView newtip;
    private View versionlayout;

    private DeviceItem mDevice;
    private TextView versiontv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_device_info);
        hideActionBar();
        mDevice = getIntent().getParcelableExtra(CommonConstants.EXT_DEVICE_ITEM);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setVersionInfo();
    }


    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    public void getDeviceVersion(){
        ImageView ivVersion = findViewById(R.id.version_image);
        if (mDevice != null){
            DeviceFirmware.INSTANCE.queryVersion(mDevice.uuid, firmwareVersionResp -> {
                if (firmwareVersionResp != null && firmwareVersionResp.getSuccess()){
                    FirmwareVersion firmwareVersion = firmwareVersionResp.getData();
                    if (firmwareVersion != null){
                        TGLog.d(TAG, "firmwareVersionResp:" + firmwareVersionResp);
                        if (firmwareVersion.getCurrent() != null ){
                            String v = StringUtils.getDeviceVersion(firmwareVersion.getCurrent().getVersion());
                            TGLog.d(TAG, "v:" + firmwareVersion.getCurrent().getVersion() + " v1:" + v);
                            versiontv.setText(v);
                        }
                        if (firmwareVersion.getAvailable() != null && !isShared()){
                            newtip.setVisibility(View.VISIBLE);
                            versionlayout.setClickable(true);
                            ivVersion.setVisibility(View.VISIBLE);
                            versionlayout.setOnClickListener(view -> {
                            });
                        }else{
                            newtip.setVisibility(View.GONE);
                            versionlayout.setClickable(false);
                            ivVersion.setVisibility(View.GONE);
                        }
                    }
                }
            });
        }


    }


    public boolean isShared(){
        if (mDevice != null){
            return mDevice.isShare();
        }
        return false;
    }
    public void setVersionInfo(){
        newtip = findViewById(R.id.newtip);
        versionlayout = findViewById(R.id.versionlayout);
        versiontv = findViewById(R.id.versiontv);
        versiontv.setText(mDevice.current_version_code);
        getDeviceVersion();
    }

    public void initView() {
        //新的需求，更改颜色
        modifyToolBar(R.string.device_info);
        findViewById(R.id.back_toolbar).setOnClickListener(v -> finish());
        findViewById(R.id.rel_device_info_id).setOnClickListener(view -> {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData   mClipData = ClipData.newPlainText("text", idtv.getText().toString());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            showToast(R.string.device_info_copy);

        });


        idtv = findViewById(R.id.idtv);
        idtv.setText(mDevice.uuid);
    }

}
